﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;
using FeaturesBlog.Models;

namespace FeaturesBlog.Helpers
{
    public class HtmlHelpers
    {
        private XmlNode _currentNode;
        private PostModel _post;
        public const string xmlPath = @"~/Content/BBTemplates.xml";
        private XmlDocument _doc;

        private const string BB_CODE = @"bbcode";
        private const string HTML_CODE = @"htmlcode";
        private const string VALUE = @"value";
        private const string NAME = @"name";

        private const string POST_ID = @"%PostId%";
        private const string POST_DATE = @"%PostDate%";
        private const string POST_SUBJECT = @"%PostSubject%";

        public HtmlHelpers()
        {
            _doc = new XmlDocument();
            _doc.Load(HttpContext.Current.Server.MapPath(xmlPath));
        }

        private string GetValueByPostTemplate(string template)
        {
            switch (template)
            {
                case POST_ID:
                    return _post.BlogPost.PostId.ToString();
                case POST_DATE:
                    return _post.BlogPost.PostDate.ToString();
                case POST_SUBJECT:
                    return _post.BlogPost.PostSubject.ToString();
                default:
                    return null;
            }
        }

        private string Replace(Regex regex, string text, MatchEvaluator me)
        {
            string txt = regex.Replace(text, me);
            return (text.GetHashCode().Equals(txt.GetHashCode())) ? txt : Replace(regex, txt, me);
        }

        private void EncodeHtmlAndConvertBBCodes()
        {
            //string text = HttpUtility.HtmlEncode(_post.BlogPost.PostBody);
            string text = _post.BlogPost.PostBody;
            Regex tag = null;
            XmlNodeList nodes = _doc.DocumentElement.ChildNodes;
            foreach(XmlNode template in nodes)
            {
                _currentNode = template;
                tag = new Regex(template.Attributes[BB_CODE].Value);
                MatchEvaluator myEvaluator = new MatchEvaluator(ReplaceLink);
                text = Replace(tag, text, myEvaluator);
            }
            _post.BlogPost.PostBody = text;
        }
        private string ReplaceLink(Match m)
        {
            if (_currentNode.Attributes[NAME].Value.Equals("cut") && !_post.PostWithActions)
            {
                return m.ToString().Replace("[cut]", string.Empty).Replace("[/cut]", string.Empty);
            }
            string replaceString = _currentNode.Attributes[HTML_CODE].Value;

            List<string> elements = new List<string>();
            Regex element = null;
            string nodeValue; string postValue = null;
            foreach (XmlNode n in _currentNode.ChildNodes)
            {
                nodeValue = n.Attributes[VALUE].Value;
                postValue = GetValueByPostTemplate(nodeValue);
                if (postValue != null)
                {
                    elements.Add(postValue);
                }
                else
                {
                    element = new Regex(nodeValue);
                    Match cs = element.Match(m.ToString());
                    if(cs.Length != 0)
                    {
                        elements.Add(cs.ToString());
                    }
                    else
                    {
                        elements.Add(string.Empty);
                    }
                }
            }
            return elements.Count != 0 ? string.Format(replaceString, elements.ToArray<string>()) : replaceString;
        }
        public PostModel EncodeHtmlForPost(PostModel p)
        {
            if (p != null)
            {
                _post = p;
                EncodeHtmlAndConvertBBCodes();
                //_post.BlogPost.PostSubject = HttpUtility.HtmlEncode(p.BlogPost.PostSubject);
                return _post;
            }
            return null;
        }
        public Comment EncodeHtmlForComment(Comment c)
        {
            c.CommentBody = HttpUtility.HtmlEncode(c.CommentBody).Replace("\r\n", "<br />");
            return c;
        }
    }
}
